package com.example.myapplication.found;

import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockBean2;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.model.Fenx11;
import com.example.myapplication.found.model.Fenxi14;
import com.example.myapplication.found.model.Fenxi2;
import com.example.myapplication.found.model.Fenxi3;
import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.example.myapplication.util.TimeUtil;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class FoundHttp {
    //创业板
    static String cybUrl = "http://58.push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery35106595695884038104_1662102556373&secid=0.399006&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&klt=101&fqt=1&end=20500101&lmt=1200000&_=1662102556378";
    //场内基金
    static String code = "510300";
    static String foundUrl = "https://push2his.eastmoney.com/api/qt/stock/kline/get?secid=1." + code + "&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61&lmt=25000&klt=101&fqt=1&end=30000101&ut=fa5fd1943c7b386f172d6893dbfba10b&cb=cb_1662086438298_23991038&cb_1662086438298_23991038=cb_1662086438298_23991038";

    public static void main(String[] args) throws Exception {
        FoundHttp foundHttp = new FoundHttp();
        foundHttp.start();
    }

    void start() {
        getKlines2(foundUrl);
//        getKlines2(cybUrl);
    }

    void test(StockBean stockBean) {
        Fenx11  fenxi = new Fenx11(stockBean,stockBean);
        fenxi.start();
        fenxi.analysis();

    }


    public void getKlines2(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("Referer", "http://fundf10.eastmoney.com/")
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    String tag1 = "\"data\":";
                    int index1 = html.indexOf(tag1);
                    int index2 = html.lastIndexOf("});");
                    html = html.substring(index1 + tag1.length(), index2);
                    StockBean2 stockBean = GsonUtil.parse(html, StockBean2.class);
                    if (stockBean == null) {
                        System.out.println("sss" + url);
                    }
                    List<StockKlineBean> klineList = new ArrayList<>();
                    for (int i = 0; i < stockBean.klines.size(); i++) {
                        String line = stockBean.klines.get(i);
                        String[] item = line.split(",");
                        StockKlineBean stockKlineBean = new StockKlineBean();
                        stockKlineBean.date = item[0];
                        stockKlineBean.date2 = TimeUtil.getTimeSS(item[0] + " 00:00:00");
                        stockKlineBean.jinKai = Float.parseFloat(item[1]);
                        stockKlineBean.shouPan = Float.parseFloat(item[2]);
                        stockKlineBean.zuigao = Float.parseFloat(item[3]);
                        stockKlineBean.zuidi = Float.parseFloat(item[4]);
                        stockKlineBean.chengjiaoliang = Double.parseDouble(item[5]);
                        stockKlineBean.chengjiaoe = Double.parseDouble(item[6]);
                        stockKlineBean.zhenfu = Float.parseFloat(item[7]);
                        stockKlineBean.chg = Float.parseFloat(item[8]);
                        stockKlineBean.zde = Float.parseFloat(item[9]);
                        stockKlineBean.huanshou = Float.parseFloat(item[10]);
                        stockKlineBean.index = i;
                        klineList.add(stockKlineBean);
                    }
                    stockBean.klineList = klineList;
                    test(stockBean);
                }
            }
        });
    }

}
